package com.bailugansan.efficiencyboard.dao.impl;

import com.bailugansan.efficiencyboard.dao.TaskDao;
import com.bailugansan.efficiencyboard.pojo.Board;
import com.bailugansan.efficiencyboard.utils.DateUtils;
import com.bailugansan.efficiencyboard.utils.Druid;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author BailuGansan
 */

@Repository
public class TaskDaoImpl implements TaskDao {
    @Override
    public boolean addTask(int pId, String drawerTitle, String drawerPriority, String drawerDeadLineYear, long drawerDeadLineTime, String drawerTaskDescript, String drawerStartYear, long drawerStartTime, int order) {
        QueryRunner qr = new QueryRunner();
        Connection conn = Druid.getConnection();
        String sql = "insert into taskboard (pid, task_title, `order`, priority, start_year, start_time, dead_line_year, dead_line_time, descript) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";

        // 将long类型的 drawDeadLineTime 和 drawerStartTime 转换为字符串存入数据库
        String changedDeadLineTime = DateUtils.long2String(drawerDeadLineTime);
        String changedStartTime = DateUtils.long2String(drawerStartTime);

        try {
            qr.update(conn, sql, pId, drawerTitle, order, drawerPriority, drawerStartYear, changedStartTime, drawerDeadLineYear, changedDeadLineTime, drawerTaskDescript);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally { // 关闭连接
            DbUtils.closeQuietly(conn);
        }

        return false;
    }

    @Override
    public boolean updateTask(int pId, int taskId, String drawerTitle, String drawerPriority, String drawerDeadLineYear, long drawerDeadLineTime, String drawerTaskDescript, String drawerStartYear, long drawerStartTime, int order) {
        QueryRunner qr = new QueryRunner();
        Connection conn = Druid.getConnection();
        String sql = "UPDATE taskboard SET task_title = ?, `order` = ?, priority = ?, start_year = ?, start_time = ?, dead_line_year = ?, dead_line_time = ?, descript = ? WHERE pid = ? AND task_id = ?";

        // 将long类型的 drawDeadLineTime 和 drawerStartTime 转换为字符串存入数据库
        String changedDeadLineTime = DateUtils.long2String(drawerDeadLineTime);
        String changedStartTime = DateUtils.long2String(drawerStartTime);

        try {
            qr.update(conn, sql, drawerTitle, order, drawerPriority, drawerStartYear, changedStartTime, drawerDeadLineYear, changedDeadLineTime, drawerTaskDescript, pId, taskId);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally { // 关闭连接
            DbUtils.closeQuietly(conn);
        }

        return false;
    }

    @Override
    public boolean deleteTask(int pId, int taskId) {
        QueryRunner qr = new QueryRunner();
        Connection conn = Druid.getConnection();
        String sql = "delete from taskboard where pid = ? and task_id = ?";

        try {
            qr.update(conn, sql, pId, taskId);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally { // 关闭连接
            DbUtils.closeQuietly(conn);
        }

        return false;
    }

    @Override
    public boolean swapTask(int drapId, int drapPid, int dropPid) {
        QueryRunner qr = new QueryRunner();
        Connection conn = Druid.getConnection();
        String sql = "update taskboard set pid = ? where pid = ? and task_id = ?";

        try {
            qr.update(conn, sql, dropPid, drapPid, drapId);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally { // 关闭连接
            DbUtils.closeQuietly(conn);
        }

        return false;
    }

    @Override
    public List<Board> getTasks(int poolId) {
        QueryRunner qr = new QueryRunner();
        Connection conn = Druid.getConnection();
        String getBoardListSql = "select * from taskboard where pid = ?";

        try {
            List<Board> boards = qr.query(conn, getBoardListSql, new BeanListHandler<>(Board.class), poolId);
            return boards;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return null;
    }
}
